package com.theten52.demo.load.util;

/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.io.File;

/**
 * Library
 * code copy form 【org.apache.tomcat.jni.Library】
 */
public final class LibraryLoader {

    public static void load(File library) throws Exception {
        //noinspection TryWithIdenticalCatches
        try {
            System.load(library.getAbsolutePath());
        } catch (ThreadDeath t) {
            throw t;
        } catch (VirtualMachineError t) {
            // Don't use a Java 7 multiple exception catch so we can keep
            // the JNI code identical between Tomcat 6/7/8/9
            throw t;
        } catch (Throwable t) {
            if (library.exists()) {
                // File exists but failed to load
                throw t;
            }
            throw new LibraryNotFoundError(library.getAbsolutePath(), t.toString());
        }
    }

    public static void loadLibrary(String libraryName) {

        String path = System.getProperty("java.library.path");
        String[] paths = path.split(File.pathSeparator);

        //noinspection TryWithIdenticalCatches
        try {
            System.loadLibrary(libraryName);
        } catch (ThreadDeath t) {
            throw t;
        } catch (VirtualMachineError t) {
            // Don't use a Java 7 multiple exception catch so we can keep
            // the JNI code identical between Tomcat 6/7/8/9
            throw t;
        } catch (Throwable t) {
            String name = System.mapLibraryName(libraryName);
            for (String s : paths) {
                File fd = new File(s, name);
                if (fd.exists()) {
                    // File exists but failed to load
                    throw t;
                }
            }
            throw new LibraryNotFoundError(libraryName, t.toString());
        }
    }


    /**
     * Calls System.load(filename). System.load() associates the
     * loaded library with the class loader of the class that called
     * the System method. A native library may not be loaded by more
     * than one class loader, so calling the System method from a class that
     * was loaded by a Webapp class loader will make it impossible for
     * other Webapps to load it.
     * <p>
     * Using this method will load the native library via a shared class
     * loader (typically the Common class loader, but may vary in some
     * configurations), so that it can be loaded by multiple Webapps.
     *
     * @param filename - absolute path of the native library
     */
    public static void systemLoad(String filename) {
        System.load(filename);
    }

    /**
     * Calls System.loadLibrary(libName). System.loadLibrary() associates the
     * loaded library with the class loader of the class that called
     * the System method. A native library may not be loaded by more
     * than one class loader, so calling the System method from a class that
     * was loaded by a Webapp class loader will make it impossible for
     * other Webapps to load it.
     * <p>
     * Using this method will load the native library via a shared class
     * loader (typically the Common class loader, but may vary in some
     * configurations), so that it can be loaded by multiple Webapps.
     *
     * @param libName - the name of the native library
     */
    public static void systemLoadLibrary(String libName) {
        System.loadLibrary(libName);
    }

}

